package org.tuio4j.util;

import java.util.ArrayList;

/**
 * See license.txt for license information.
 * @author Uwe Laufs
 * @version 1.0
 */
public class Flag {
	private boolean flag;
	private ArrayList<FlagListener> listeners = new ArrayList<FlagListener>();
	
	public Flag(){
		flag = false;
	}
	public synchronized void setValue(boolean flag){
		if(flag!=this.flag){
			this.flag = flag;
			this.fireChangeEvent();
		}
	}
	public synchronized boolean getValue(){
		return flag;
	}
	public synchronized void toggle(){
		this.setValue(!this.flag);
	}
	public void addListener(FlagListener fl){
		if(!this.listeners.contains(fl)){
			this.listeners.add(fl);
		}
	}
	public void removeListener(FlagListener fl){
		if(this.listeners.contains(fl)){
			this.listeners.remove(fl);
		}
	}
	private synchronized void fireChangeEvent(){
		for (int i = 0; i < this.listeners.size(); i++) {
			listeners.get(i).flagChanged(this.getValue());
		}
	}
}
